<template>
  <div>
    <el-button type="primary" @click="willAdd">添加</el-button>
    <list-vue :list="list" @init="init" @willEdit="willEdit"></list-vue>
    <form-vue :info="info" @init="init" ref="form"></form-vue>
    <el-pagination
      layout="prev, pager, next"
      :total="total"
      background
      :page-size="size"
      @current-change="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import listVue from "../manage/components/list.vue";
import formVue from "../manage/components/form.vue";
import { mapGetters, mapActions } from "vuex";
import { requsercount, requserlist } from "../../http/api";
export default {
  components: {
    listVue,
    formVue,
  },
  props: [],
  data() {
    return {
      //点击弹出form表单数据
      list: [],
      total: 0,
      size: 2,
      page: 1,
      info: {
        isshow: false,
        isAdd: true,
      },
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {
    //请求列表
    this.init();
  },
  methods: {
    ...mapActions({}),
    init() {
      this.reqList();
      this.reqTotal();
    },
    reqList() {
      //请求列表
      requserlist({ size: this.size, page: this.page }).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          let arr = res.data.list ? res.data.list : [];
          if (arr.length == 0 && this.page !== 1) {
            this.page--;
            this.reqList();
            return;
          }
          this.list = arr;
        }
      });
    },
    //总页数获取
    reqTotal() {
      requsercount().then((res) => {
        // console.log(res);
        this.total = res.data.list[0].total;
      });
    },
    //点击页数变化
    changePage(e) {
      this.page = e;
      this.reqList();
    },
    willAdd() {
      this.info.isshow = true;
      this.info.isAdd = true;
    },
    willEdit(id) {
      this.info.isshow = true;
      this.info.isAdd = false;
      this.$refs.form.getOne(id);
    },
  },
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
</style>